package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.service.goods.GoodsService;
import com.dongdongshop.service.goods_jpa.GoodsJpaService;
import com.dongdongshop.service.itemCat.ItemCatService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Reference
    private GoodsService goodsService;
    @Reference
    private ItemCatService itemCatService;
    @Reference
    private GoodsJpaService goodsJpaService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @RequestMapping("goodsAuditShow")
    public String goodsAuditShow(){
        return "admin/goods";
    }

    @RequestMapping("listGoodsByUnreviewed")
    @ResponseBody
    private Result listGoodsByUnreviewed(){
        /*List<TbGoods> tbGoodsList = goodsService.listGoodsByUnreviewed();*/
        List<TbGoods> tbGoodsList = goodsJpaService.listGoodsByUnreviewed();
        for (TbGoods tbGoods : tbGoodsList) {
            List<TbItemCat> list = itemCatService.listCategory(tbGoods.getCategory3Id());
            tbGoods.setItemCatList(list);
        }
        return Result.ok().setData(tbGoodsList);
    }
    //申请通过
    @RequestMapping("updateAuditToPass")
    @ResponseBody
    public Result updateAudit(@RequestParam("ids[]")Long[] ids){
        rocketMQTemplate.convertAndSend("goods-status",ids);
        rocketMQTemplate.convertAndSend("item-create-es",ids);
        /*int i = goodsService.updateAuditToPass(ids);*/
        goodsJpaService.updateAuditToPass(ids);
        return Result.ok();
    }
}
